package io.yunho.yunho.interview.two.thread;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author: yunho
 * @date: 2021/11/24 15:39
 * @description:
 */
public class ThreadWithOutPool {
    public static void main(String[] args) {
        ThreadWithOutPoolOb threadWithOutPoolOb = new ThreadWithOutPoolOb();
//        new Thread(threadWithOutPoolOb).start();
//        new Thread(threadWithOutPoolOb).start();
        //1. 创建线程池
        ExecutorService pool = Executors.newFixedThreadPool(5);
        List<String> d = Arrays.asList("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
        // 数据量大小
        int length = d.size();
        // 每个线程处理的数据个数
        int taskCount = length / 5;
        List<Future<List<String>>> r = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            // 每个线程任务数据list
            List<String> subData = null;
            if (i == (4)) {
                subData = d.subList(i * taskCount, length);
            } else {
                subData = d.subList(i * taskCount, (i + 1) * taskCount);
            }
            HandleCallable<String> handleCallable = new HandleCallable<>(Thread.currentThread().getName()+i,d);
            // 将线程加入到线程池
            Future<List<String>> f = pool.submit(handleCallable);
            r.add(f);
        }
//		//2. 为线程池中的线程分配任务
//		for (int i = 0; i < 10; i++) {
//			pool.submit(threadWithOutPoolOb);
//		}
//
		//3. 关闭线程池
		pool.shutdown();


        r.stream().forEach(x->{
            try {
                x.get().stream().forEach(System.out::println);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    }
}

class ThreadWithOutPoolOb implements Runnable{
    private int i =0;
    @Override
    public void run() {
        while (i<=100) {
            System.out.println(Thread.currentThread().getName() + "\t" +i++);
        }
    }
}
